package doc2db.table;

import java.util.ArrayList;
import java.util.List;

public class DocTableBody {
	
	private List <DocTableRows> rows = new ArrayList<DocTableRows>();
	private int current = 0;
	
	public DocTableBody (){
		rows.add(new DocTableRows());
	}
	
	public DocTableRows getCurrentRows(){
		return rows.get(current);
	}
       
    public DocTableRows getRows(int i) {
        return rows.get(i);
    }

	public void createNewRows() {
		boolean tr = rows.add(new DocTableRows());
		current++;
	}

	public int size() {
		return rows.size();
	}
        
    public int getRowsSize(int i) {
        if (i < 0 || i > size()) {
            return -1;
        }
        return rows.get(i).size();
    }

	public String[] getInsertSQLString(String tableName) {
		String[] s = new String[rows.size()];
		int i = 0;
		for(DocTableRows row: rows){
			if (row != null && row.size() > 0) {
				StringBuffer a = new StringBuffer();
				a.append("INSERT INTO ");
				a.append(tableName);
				a.append(" (");
				a.append(row.getWhereInsertSQLString());
				a.append(" ) ");
				a.append(" VALUES ");
				a.append(" (");
				a.append(row.getInsertSQLString());
				a.append(" );");
				s[++i] = a.toString();
			}
		}
		
		return s;
	}

    @Override
    public String toString() {
        StringBuffer ret = new StringBuffer();
        int i = 0;
        for (DocTableRows c : rows)
            ret.append("" + (i++) + ") " + c.toString() + "\n");
        return ret.toString();
    }
	
}
